package com.aigou.admin.aigouspringboot.controller.admin;

import com.aigou.admin.aigouspringboot.common.LogScope;
import com.aigou.admin.aigouspringboot.entity.ShopCartProduct;
import com.aigou.admin.aigouspringboot.result.Result;
import com.aigou.admin.aigouspringboot.service.ShopCartProductService;
import com.aigou.admin.aigouspringboot.utils.RandomUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/shopCartProduct")
@Slf4j
public class ShopCartProductController {

    @Resource
    private ShopCartProductService shopCartProductService;

    @GetMapping
    @LogScope(title = "购物车商品管理模块", content = "查看购物车商品列表信息")
    public Result select(@RequestParam Integer current) {

        log.info("查询第{}页的用户信息", current);
        // 分页查询 都返回PageInfo(分页插件工具)
        PageInfo<ShopCartProduct> userPageInfo = shopCartProductService.selectShopCartProductListByPage(current);
        return Result.success(userPageInfo);
    }

    @GetMapping("/{id}")
    @LogScope(title = "购物车商品管理模块", content = "根据id查询购物车商品信息")
    public Result detail(@PathVariable String id) {

        ShopCartProduct shopCartProduct = shopCartProductService.selectById(id);
        return Result.success(shopCartProduct);
    }

    @GetMapping("/user/{userId}")
    @LogScope(title = "购物车商品管理模块", content = "根据userid查询购物车商品信息")
    public Result get(@PathVariable String userId) {

        List<ShopCartProduct> shopCartProductList = shopCartProductService.selectByUserID(userId);
        return Result.success(shopCartProductList);
    }

    @DeleteMapping("/{id}")
    @LogScope(title = "购物车商品管理模块", content = "根据id删除购物车商品信息")
    public Result delete(@PathVariable String id) {

        int i = shopCartProductService.deleteById(id);
        return i > 0 ? Result.success() : Result.error();
    }



    @PutMapping
    @LogScope(title = "购物车商品管理模块", content = "编辑购物车商品信息")
    public Result edit(@RequestBody ShopCartProduct shopCartProduct) {

        int i = shopCartProductService.edit(shopCartProduct);
        return i > 0 ? Result.success() : Result.error();
    }

    @PostMapping("/save")
    @LogScope(title = "购物车商品管理模块", content = "添加购购物车商品信息")
    public Result save(@RequestBody ShopCartProduct shopCartProduct) {

        shopCartProduct.setId(RandomUtil.getRandomNumber());
        int i = shopCartProductService.save(shopCartProduct);
        return i > 0 ? Result.success() : Result.error();
    }


}
